package com.hy.jedis.factory;

import com.hy.jedis.properties.JedisProperties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2023/7/21
 * Time:16:29
 */
@Component
public class JedisConnectionFactory {

    @Resource
    private JedisProperties jedisProperties;

    public Jedis getJedis(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大连接数
        jedisPoolConfig.setMaxTotal(10);
        // 最大空闲连接
        jedisPoolConfig.setMaxIdle(10);
        // 最小空闲连接
        jedisPoolConfig.setMinIdle(0);
        // 设置最长等待时间，ms
        jedisPoolConfig.setMaxWait(Duration.ofMillis(200));
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, jedisProperties.getHost(),
                Integer.valueOf(jedisProperties.getPort()),
                1000,
                jedisProperties.getPassword());
        return jedisPool.getResource();
    }
}
